{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "70f896f3-db14-4886-a9f0-f8d8200c6e4a",
   "metadata": {},
   "source": [
    "# Quantum Fourier Transform\n",
    "\n",
    "The quantum Fourier transform (QFT) function is the quantum analog for \n",
    "discrete Fourier transform. It is applied on the quantum register state vector.\n",
    "\n",
    "The state vector `x` is transformed to `y` in the following manner:\n",
    "\n",
    "$$\n",
    "y_{k} = \\frac{1}{\\sqrt{N}} \\sum_{j=0}^{N-1} x_j e^{2\\pi i \\frac{jk}{N}}\n",
    "$$\n",
    "\n",
    "Function: `qft`\n",
    "\n",
    "Arguments:\n",
    "\n",
    "- `target`: `QArray[QBit]`\n",
    "\n",
    "The `target` quantum argument is the quantum state on which we apply the QFT."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4aaa84d1-36c5-4eb3-a713-593d9cc509b3",
   "metadata": {},
   "source": [
    "## Example"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "d67855d0-5461-4d8e-9f7d-1bc47ca80928",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-05-07T13:25:38.777032Z",
     "iopub.status.busy": "2024-05-07T13:25:38.776008Z",
     "iopub.status.idle": "2024-05-07T13:25:41.641936Z",
     "shell.execute_reply": "2024-05-07T13:25:41.641199Z"
    }
   },
   "outputs": [],
   "source": [
    "from classiq import Output, QArray, QBit, allocate, create_model, qft, qfunc\n",
    "\n",
    "\n",
    "@qfunc\n",
    "def main(x: Output[QArray[QBit]]):\n",
    "\n",
    "    allocate(4, x)\n",
    "    qft(x)\n",
    "\n",
    "\n",
    "qmod = create_model(main)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "4e9bb384-4af0-4ab4-9e2e-2b8ba243ad5e",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-05-07T13:25:41.645346Z",
     "iopub.status.busy": "2024-05-07T13:25:41.644797Z",
     "iopub.status.idle": "2024-05-07T13:25:43.974653Z",
     "shell.execute_reply": "2024-05-07T13:25:43.973946Z"
    }
   },
   "outputs": [],
   "source": [
    "from classiq import synthesize, write_qmod\n",
    "\n",
    "write_qmod(qmod, \"qft\")\n",
    "qprog = synthesize(qmod)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
